﻿using NetMQ;
using NetMQ.Sockets;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZHello.MQ;

namespace ZHello.Net.MQ.netmq
{
    internal class NetMQPubServer : IPubServer
    {
        private PublisherSocket Socket { get; set; }

        private List<string> BindingAddrs { get; set; }

        public NetMQPubServer(string addr)
        {
            BindingAddrs = new List<string>();
            Socket = new PublisherSocket();
            Socket.Bind(addr);
            BindingAddrs.Add(addr.Trim().ToLower());
        }

        public void BindExtraAddress(string addr)
        {
            Socket.Connect(addr);
        }

        public bool Publish(string str, out string error)
        {
            if (str == null)
            {
                error = "不能推送NULL";
                return false;
            }
            error = null;
            Socket.SendFrame(str);
            return true;
        }

        public void UnBindPubAddress(string addr)
        {
            Socket.Disconnect(addr);
        }

        public void Dispose()
        {
            Socket.Close();
            Socket.Dispose();
        }
    }
}
